<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/10/28
 * Time: 20:01
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Auth as AuthModel;
use app\common\model\User as UserModel;
use think\facade\Env;
use think\facade\Request;
use think\Db;

class Index extends AdminBase
{
    public function index(){
        $curruser = UserModel::instance()->getLoginUser();
        if(Request::isAjax()){
            $auths = AuthModel::instance()->getUserAuthList($curruser['id']);
            $trees = AuthModel::instance()->getTreeMenu($auths,'');
            $this->success('菜单列表','',$trees);
        }else{
            $this->assign('curruser',$curruser);
            return $this->fetch();
        }
    }

    public function main(){
        $curruser = UserModel::instance()->getLoginUser();
        $this->assign('curruser',$curruser);
        $info = array(
            '操作系统'=>PHP_OS,
            '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式'=>php_sapi_name(),
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
        );
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function userinfo(){
        $userinfo = UserModel::instance()->getLoginUser();
        if(Request::isPost()){
            $form = Request::param('form/a');
            if(empty($form['password']))
            {
                unset($form['password']);
            }else{
                $form['password'] = md5(md5($form['password']).$userinfo['salt']);
            }
            UserModel::instance()->where(['id'=>$userinfo['id']])->update($form);
            $this->success('修改成功','',$form);
        }else{
            $userinfo = UserModel::instance()->where(['id'=>$userinfo['id']])->findOrEmpty();
            $this->assign('curruser',$userinfo);
            return $this->fetch();
        }
    }


    public function login(){
        if(Request::instance()->isPost()){
            $usermodel = new UserModel();
            $form = Request::instance()->param('form/a');
            $login = $usermodel->login($form);
            if($login)
            {
                $this->success('登录成功',url('admin/index/index'));
            }
            else{
                $this->error('登录失败');
            }
        }else{
            return $this->fetch();
        }
    }

    public function logout(){
        UserModel::instance()->logout();
        $this->success('退出登录',url('admin/index/index'));
    }

    public function noauth(){
        $this->error('无权限','','',3000);
    }

}